<template>
  <div class="courseIntroduction">
    <div>
      <h3><img src="../../image/tit.png" alt=""  width="9" height="9">老师简介</h3>
      <!-- <h3 v-if='infoObj.uid'><img src="../../image/title.png" alt="">老师简介</h3> -->
      <!-- <div class="teacherDetail" v-if='infoObj.uid'> -->
      <div class="teacherDetail">
        <img :src="infoObj.teacher_pic" width="34" height="34" />
        <div>
          <span>{{infoObj.teacher_intro}}</span>
          <!-- <p>{{infoObj.content}}</p> -->
        </div>
        <a href="javascript:;" class="focus" @click='concernTeacher(infoObj.uid)' v-show="!isConcern && !isTeacher">
          <img src="../../image/attention.png" width="74" height="33" alt="">
        </a>
        <a href="javascript:;" class="focus" @click='cancelConcern(infoObj.uid)' v-show="isConcern">
          <img src="../../image/atten_success.png" width="74" height="33" alt="">
        </a>
      </div>
      <!-- <div class="border-8"></div> -->
      <h3><img src="../../image/tit.png" alt=""  width="9" height="9">课程简介</h3>
      <!-- <img :src="infoObj.course_pic" width="34" height="34" /> -->
      <div class="content">
        {{infoObj.program}}
      </div>
    </div>
    <div class="btn">
      <a @click='collectCourse' v-show="!isCollect">

        点击收藏
      </a>
      <a @click='cancelCollect' v-show="isCollect">
        收藏成功
      </a>
      <!-- <a class="add" v-show='!isTakeIn' @click='takeInCourse'>
              参加课时
            </a>
            <a class="add" v-show='isTakeIn'>
              已参与
            </a> -->
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        infoObj: {},
        cid: '', //课程id
        isConcern: false, //是否关注 默认未关注
        isCollect: false, //是否收藏 默认未收藏
        isTeacher: false, //是否用户和该课程老师是同一人 默认否
        userid: '',
        isTakeIn: false, //用户是否参与课程
        tid: '',
        course_num: {
          single: '',
          series: ''
        }
      }
    },
    methods: {
      isLogin() {
        if (!this.userid) {
        //   this.$router.push({name:'login1'}) 
          this.$utils._toast('请先登录!');
          return false;
        } else {
          return true;
        }
      },
      // 判断用户和该课程老师是否同一人
      justIsTeacher(tid, userid) {
        if (tid == userid) {
          this.isTeacher = true;
        }
      },
      //根据课程id获取课程信息
      getDetailsData() {
        this.cid = this.$route.query.id;
        this.$api.get(this.$api.baseURLSchool, "select_cid/view_course_user/" + this.cid, {}, r => {
          if (r.data.ret == 1) {
            this.infoObj = r.data.info;
            this.tid = r.data.info.uid;
            var tid = r.data.info.uid;
            this.userid = localStorage.getItem("loginMobile");
            if (this.userid) {
              this.justIsTeacher(tid, this.userid);
              this.justIsConcern(tid);
            }
            this.getCourseNumByUid(tid);
          }
        }, err => {
          console.log(err);
        })

      },
      //根据userid和tid查看用户是否收关注老师
      justIsConcern(tid) {
        this.$api.post(this.$api.baseURLSchool, "/selecttype/attention/0?userid=" + localStorage.getItem("loginMobile") +
          "&type=fans&id=" + tid, {}, r => {
            if (r.data.records > 0) {
              this.isConcern = true;
            } else {
              this.isConcern = false;
            }
          }, err => {
            console.log(err);
          })
      },
      //关注老师
      concernTeacher(tid) {
        if (!this.isLogin()) {
          return false;
        }
        this.$api.post(this.$api.baseURLSchool, "/play_only/attention?userid=" + localStorage.getItem("loginMobile") +
          "&type=fans&id=" + tid, {}, r => {
            if (r.data.ret1 == 1) {
              this.$utils._toast("关注成功");
              this.isConcern = true;
            } else if (r.data.ret1 == -1) {
              this.$utils._toast("关注失败");
            } else if (r.data.ret1 == 0) {
              this.$utils._toast("已经关注过")
            }

          }, err => {
            console.log(err);
          })
      },
      //取消关注
      cancelConcern(tid) {
        this.$api.post(this.$api.baseURLSchool, "play_cancel/attention?userid=" + localStorage.getItem("loginMobile") +
          "&type=fans&id=" + tid, {}, r => {
            if (r.data.ret1 == 1) {
              this.$utils._toast("取消关注成功");
              this.isConcern = false;
            }
          }, err => {
            console.log(err);
          })
      },
      //根据userid和cid查看用户是否收藏课程
      justIsCollect() {
        // this.cid = this.$route.query.id;
        this.$api.post(this.$api.baseURLSchool, "/selecttype/attention/0?userid=" + localStorage.getItem("loginMobile") +
          "&type=collect&id=" + this.cid, {}, r => {
            if (r.data.records > 0) {
              this.isCollect = true;
            } else {
              this.isCollect = false;
            }
          }, err => {
            console.log(err);
          })
      },
      //收藏课程
      collectCourse() {
        if (!this.isLogin()) {
          return false;
        }
        // var cid = this.$route.query.id;
        var teacherName=localStorage.getItem('doctorName')
        this.$api.post(this.$api.baseURLSchool, "/play_only/attention?userid=" + localStorage.getItem("loginMobile") +
          "&type=collect&id=" + this.cid+"&teacherName="+teacherName, {}, r => {
            if (r.data.ret1 == 1) {
              this.$utils._toast("收藏成功");
              this.isCollect = true;
            } else if (r.data.ret1 == -1) {
              this.$utils._toast("收藏失败");
            } else if (r.data.ret == 0) {
              this.$utils._toast("已经收藏过")
            }

          }, err => {
            console.log(err);
          })
      },
      //取消收藏
      cancelCollect() {
        // var cid = this.$route.query.id;
        this.$api.post(this.$api.baseURLSchool, "play_cancel/attention?userid=" + localStorage.getItem("loginMobile") +
          "&type=collect&id=" + this.cid, {}, r => {
            if (r.data.ret1 == 1) {
              this.$utils._toast("取消收藏成功");
              this.isCollect = false;
            }
          }, err => {
            console.log(err);
          })
      },
      // 判断用户是否参与该课程
      justIsTakeIn() {
        this.$api.get(this.$api.baseURLSchool, "selectdata2/user_relative?user_id=" + localStorage.getItem(
          "loginMobile") + "&type_name=app8&type=app8&name=person21&rel_id=" + this.cid, {}, r => {
          if (r.data.ret == 1) {
            if (r.data.info.length > 0) {
              this.isTakeIn = true;
            }
          }
        }, err => {
          console.log(err);
        })
      },
      // 参加课时
      //   takeInCourse() {
      //     if (!this.isLogin()) {
      //       return false;
      //     }
      //     const content = {
      //       vid: '',
      //       progress: 0
      //     };
      //     console.log(content);
      //     this.$api.post(this.$api.baseURLSchool, "watch/user_relative?user_id=" + localStorage.getItem("loginMobile") +
      //       "&type_name=app8&type=app8&name=person21&rel_id=" + this.cid, content, r => {
      //         console.log(r);
      //         this.justIsTakeIn();
      //       }, err => {
      //         console.log(err);
      //       });
      //   },
      // 获取老师单节课和系列课节数
      getCourseNumByUid(tid) {
        this.$api.get(this.$api.baseURLSchool, "selectdata2/view_course_user?uid=" + tid + "&course_num=1", {}, r => {
          if (r.data.ret == 1) {
            this.course_num.single = r.data.info.length;
          }
        }, err => {
          console.log(err);
        })

        this.$api.get(this.$api.baseURLSchool, "selectdata2/view_course_user?uid=" + tid + "&course_num=2", {}, r => {
          if (r.data.ret == 1) {
            this.course_num.series = r.data.info.length;
          }
        }, err => {
          console.log(err);
        })
      }

    },
    created() {
      this.getDetailsData();
      this.justIsCollect();
      this.justIsTakeIn();
    }
  }

</script>
<style lang="scss" scoped>
  .content {
    min-height: 100px;
  }

</style>
